package com.gitee.cirnochat.common.utils;

import com.gitee.cirnochat.file.model.enums.FileType;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.UUID;

import static com.gitee.cirnochat.common.constant.FileConstant.*;


/**
 * 文件工具类
 *
 * @author FlanChan
 * @since 2025-05-05 19:02:05
 */
@Slf4j
public final class FileUtil {
    private FileUtil() {
        // 私有构造函数，禁止实例化
        throw new UnsupportedOperationException("FileUtil cannot be instantiated");
    }

    /**
     * 将inputStream转化为file
     *
     * @param is
     * @param file 要输出的文件目录
     */
    public static void inputStream2File(InputStream is, File file) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            int len = 0;
            byte[] buffer = new byte[8192];

            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } finally {
            os.close();
            is.close();
        }
    }

    /**
     * 获取文件扩展名
     *
     * @param filename 文件名
     * @return 扩展名（不带点）
     */
    public static String getExtension(String filename) {
        if (filename == null || filename.isEmpty()) {
            return "";
        }
        int lastIndexOfDot = filename.lastIndexOf('.');
        if (lastIndexOfDot == -1 || lastIndexOfDot == filename.length() - 1) {
            return ""; // No extension found
        }
        return filename.substring(lastIndexOfDot + 1);
    }

    /**
     * 获取文件类型
     *
     * @param contentTypeStr 内容类型字符串
     * @return
     */
    public static FileType getFileTypeByContentType(String contentTypeStr) {
        String mimeType = contentTypeStr.toLowerCase();
        if (mimeType.startsWith("image/")) {
            return FileType.IMAGE;
        } else if (mimeType.startsWith("audio/")) {
            return FileType.AUDIO;
        } else if (mimeType.startsWith("video/")) {
            return FileType.VIDEO;
        } else {
            return FileType.FILE;
        }
    }

    public static String getFileUploadPath(FileType fileType) {
        return switch (fileType) {
            case IMAGE -> IMAGE_UPLOAD_PREFIX_PATH;
            case AUDIO -> AUDIO_UPLOAD_PREFIX_PATH;
            case VIDEO -> VIDEO_UPLOAD_PREFIX_PATH;
            default -> FILE_UPLOAD_PREFIX_PATH;
        };
    }

    public static String generateRandomFileName() {
        return UUID.randomUUID().toString().replace("-", "").substring(0,20);
    }

    public static String getExtensionByContentType(String contentType) {
        if (contentType == null || contentType.isEmpty()) {
            return "";
        }
        String mimeType = contentType.toLowerCase();
        if (mimeType.startsWith("image/")) {
            return mimeType.substring("image/".length());
        } else if (mimeType.startsWith("audio/")) {
            return mimeType.substring("audio/".length());
        } else if (mimeType.startsWith("video/")) {
            return mimeType.substring("video/".length());
        }
        return "";
    }
}
